package com.biostime.qdingding.modules.cms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.cms.entity.Channel;
import com.biostime.qdingding.modules.cms.service.ChannelService;
import com.biostime.qdingding.modules.sys.entity.User;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 频道表Controller
 * @author king
 * @version 2015-12-11
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/channel")
public class ChannelController extends BaseController {

	@Autowired
	private ChannelService channelService;
	
	@ModelAttribute
	public Channel get(@RequestParam(required=false) String id) {
		Channel entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = channelService.get(id);
		}
		if (entity == null){
			entity = new Channel();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:channel:view")
	@RequestMapping(value = {"list", ""})
	public String list(Channel channel, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Channel> page = channelService.findPage(new Page<Channel>(request, response), channel); 
		model.addAttribute("page", page);
		return "modules/cms/channelList";
	}

	@RequiresPermissions("cms:channel:view")
	@RequestMapping(value = "form")
	public String form(Channel channel, Model model) {
		model.addAttribute("channel", channel);
		return "modules/cms/channelForm";
	}

	@RequiresPermissions(value={"cms:channel:edit","cms:channel:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Channel channel, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, channel)){
			return form(channel, model);
		}
		
		User curUser = UserUtils.getUser();
		
		channel.setCenter(curUser.getCenter());
		channelService.save(channel);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/cms/channel/?repage";
	}
	
	@RequiresPermissions("cms:channel:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Channel channel, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				channelService.delete(new Channel(id));
			}
		}else{
			channelService.delete(channel);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/cms/channel/?repage";
	}

}